package com.shuoyue.ycgk.ui.main.today_count;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.CourseInfo;
import com.shuoyue.ycgk.entity.TodayCourse;
import com.shuoyue.ycgk.entity.TodayCourseInfo;
import com.shuoyue.ycgk.entity.TodayQuestionInfo;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;

import butterknife.BindView;

public class FragmentTodayCourse extends BaseMvpFragment<TodayLearnContract.Presenter>
        implements TodayLearnContract.View {
    int moduleId;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    TodayCourseAdapter adapter;
    ListWithPage listWithPage;


    public static FragmentTodayCourse getInstance(int moduleId) {
        FragmentTodayCourse fragmentTodayCourse = new FragmentTodayCourse();
        Bundle bundle = new Bundle();
        bundle.putInt("moduleId", moduleId);
        fragmentTodayCourse.setArguments(bundle);
        return fragmentTodayCourse;
    }


    @Override
    public void initParams(Bundle bundle) {
        moduleId = bundle.getInt("moduleId");
    }

    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_today_course;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new TodayCourseAdapter(null);
        recycleView.setAdapter(adapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getTodayInfo(moduleId, 1, 2);
            refreshLayout.finishRefresh();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getTodayInfo(moduleId, listWithPage.getCurrent() + 1, 2);
            refreshLayout.finishLoadMore();
        });
        registEmptyView(adapter);
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            TodayCourse todayCourse = (TodayCourse) adapter.getItem(position);
            startActivity(new Intent(mContext, CourseInfoActivity.class)
            .putExtra("id",todayCourse.getId()));
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new TodayLearnContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getTodayInfo(moduleId, 1, 2);
    }

    @Override
    public String getTitle() {
        return "课程学习";
    }

    @Override
    public void setTodayQuestionInfo(TodayQuestionInfo todayQuestionInfo) {
    }

    @Override
    public void setTodayCourseInfo(TodayCourseInfo list) {
        time.setText(String.valueOf(list.getTotalTime()));
        listWithPage = list.getPage();
        if (list.getPage().getCurrent() == 1) {
            adapter.resetData(list.getPage().getRecords());
        } else {
            adapter.addData(list.getPage().getRecords());
        }
        refreshLayout.setEnableLoadMore(list.getPage().getCurrent() != list.getPage().getPages()&& list.getPage().getPages() > 0);
    }
}
